Ansible ad-hoc commands are one-time, quick execution commands that allow you to perform simple tasks on remote hosts without writing a full playbook. They are used for immediate actions like rebooting servers, installing packages, checking system status, or managing services. Ad-hoc commands are typically run from the command line and are a fast and efficient way to execute individual tasks on one or more nodes.
No Playbook Required: Unlike playbooks, which are written in YAML and can involve complex logic, ad-hoc commands are simple, single-line commands that don’t need any YAML configuration.
Task-specific: They are used to perform specific tasks like managing services, packages, users, files, or gathering system information.
Quick Execution: These commands are useful for quick, on-the-fly tasks that don’t require complex automation.
ansible <host-pattern> -m <module> -a "<arguments>" [options]
<host-pattern> specifies the hosts or group of hosts from the inventory.
-m <module> specifies the Ansible module to use (like ping, shell, apt, yum, service, etc.).
-a "<arguments>" provides the arguments required by the module.
[options] are additional flags, such as -b for privilege escalation (sudo).